import BaseVenueProcessor from './BaseVenueProcessor.js';
import { getZhuoLeVenueSettings } from '../request.js';

/**
 * 库卡网球场地数据处理器
 */
export class ZhuoLeProcessor extends BaseVenueProcessor {
  constructor() {
    super();
    this.venueName = '库卡网球';
  }

  /**
   * 获取库卡网球场地预约设置
   * @param {Object} params - 查询参数 { userId, projectUid, dateTime }
   * @returns {Promise} 请求Promise
   */
  async getVenueSettings(params = {}) {
    // 设置默认参数
    const defaultParams = {
      userId: 5710920,
      projectUid: "1725630736098549713",
      dateTime: "2025-08-13"
    };
    
    // 合并参数
    const requestData = { ...defaultParams, ...params };
    
    return await getZhuoLeVenueSettings(requestData);
  }

  /**
   * 验证数据是否有效
   * @param {Object} data - 原始场地数据
   * @returns {boolean} 数据是否有效
   */
  validateData(data) {
    return data && 
           data.result && 
           data.result.slots && 
           Array.isArray(data.result.slots);
  }

  /**
   * 按场地名称分组
   * @param {Object} data - 原始场地数据
   * @returns {Map} 场地Map
   */
  groupVenues(data) {
    const venueMap = new Map();
    
    // 遍历时间槽数据，按场地名称分组
    data.result.slots.forEach(slot => {
      const venueName = slot.classRoomName;
      if (!venueMap.has(venueName)) {
        venueMap.set(venueName, {
          name: venueName,
          timeSlots: []
        });
      }
    });
    
    return venueMap;
  }

  /**
   * 处理时间槽
   * @param {Map} venueMap - 场地Map
   * @param {Object} data - 原始场地数据
   */
  processTimeSlots(venueMap, data) {
    venueMap.forEach((venue, venueName) => {
      // 遍历时间槽数据，找到对应场地的时间槽
      data.result.slots.forEach(slot => {
        if (slot.classRoomName === venueName) {
          const beginDatetime = slot.beginDatetime;
          const endDatetime = slot.endDatetime;
          const timeSlot = `${beginDatetime.substring(11, 16)}-${endDatetime.substring(11, 16)}`;
          
          // 不在后端进行价格转换，保留原始价格和时长信息
          // 让前端的convertToHourlyPrice函数来处理价格转换
          const gapMinutes = slot.gapMinutes || 60; // 默认60分钟
          
          // 添加时间槽信息
          venue.timeSlots.push({
            time: timeSlot,
            startTime: beginDatetime.substring(11, 16), // 添加startTime
            endTime: endDatetime.substring(11, 16), // 添加endTime
            isFree: slot.status === 0, // status为0表示可预约
            price: slot.cost, // 保留原始价格，不进行转换
            originalPrice: slot.cost, // 保留原始总价格
            duration: gapMinutes, // 保留时间段长度信息
            beginDatetime: beginDatetime,
            endDatetime: endDatetime
          });
        }
      });
    });
  }

  /**
   * 转换为场地列表
   * @param {Map} venueMap - 场地Map
   * @returns {Array} 场地列表
   */
  convertToVenueList(venueMap) {
    return Array.from(venueMap.values()).map((venue, index) => {
      return {
        id: 1000 + index, // 给库卡场地分配ID
        name: venue.name,
        address: '北京市朝阳区库卡大厦',
        latitude: 39.9042,
        longitude: 116.4074,
        phone: '010-12345678',
        rating: 4.8,
        price: 0,
        facilities: ['室内场地', '专业教练', '器材租赁'],
        image: '',
        distance: 1.2,
        timeSlots: venue.timeSlots,
        dataSource: this.venueName
      };
    });
  }
}

export default ZhuoLeProcessor;